<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px" inline>
      <el-form-item label="卡券名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="卡券名称"></el-input>
      </el-form-item>
      <el-form-item label="分类id" prop="categoryId">
        <el-select v-model="dataForm.categoryId" :disabled="disabled" clearable filterable placeholder="请选择"
                   class="width185">
          <el-option v-for="item in cardCategoryList"
                     :label="item.name"
                     :key="item.id"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="卡券标签" prop="tag">
        <el-select v-model="dataForm.tag" :disabled="disabled" clearable filterable multiple placeholder="请选择"
                   class="width185" @onChange="typeUpdata">
          <el-option
            v-for="tag in tags"
            :key="tag.id"
            :label="tag.name"
            :value="tag.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图片地址" prop="imgUrl">
        <el-img v-model="dataForm.imgUrl" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="卡券简介" prop="cardDesc">
        <el-input v-model="dataForm.summary" :disabled="disabled" placeholder="卡券简介"></el-input>
      </el-form-item>
      <el-form-item label="购买起有效天数" prop="expireDay">
        <el-input-number v-model="dataForm.expireDay" :disabled="disabled" placeholder="购买起有效天数"></el-input-number>
      </el-form-item>
      <el-form-item label="卡券背景图" prop="backImg">
        <el-img v-model="dataForm.backImg" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="背景色" prop="backColor">
        <el-input v-model="dataForm.backColor" :disabled="disabled" placeholder="背景色"></el-input>
      </el-form-item>
      <el-form-item label="2张图片上" prop="upImg">
        <el-img v-model="dataForm.upImg" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="2张图片下" prop="downImg">
        <el-img v-model="dataForm.downImg" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="卡券库存" prop="cardNumber">
        <el-input-number v-model="dataForm.cardNumber" :disabled="disabled" placeholder="卡券库存"></el-input-number>
      </el-form-item>
      <el-form-item label="卡券价值" prop="marketPrice">
        <el-input-number v-model="dataForm.marketPrice" :disabled="disabled" placeholder="卡券价值"></el-input-number>
      </el-form-item>
      <el-form-item label="银联支付售价" prop="retailPrice">
        <el-input-number v-model="dataForm.retailPrice" :disabled="disabled" placeholder="售价"></el-input-number>
      </el-form-item>
      <el-form-item label="微信支付售价" prop="wechatPrice">
        <el-input-number v-model="dataForm.wechatPrice" :disabled="disabled" placeholder="售价"></el-input-number>
      </el-form-item>
      <el-form-item label="补贴" prop="subsidy">
        <el-input-number v-model="dataForm.subsidy" :disabled="disabled" placeholder="补贴"></el-input-number>
      </el-form-item>
      <el-form-item label="状态" prop="isOnSale">
        <el-radio-group v-model="dataForm.isOnSale" :disabled="disabled">
          <el-radio :label="0">下架</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :disabled="disabled" placeholder="排序"></el-input-number>
      </el-form-item>
      <el-form-item label="抽奖概率" prop="probability">
        <el-input v-model="dataForm.probability" :disabled="disabled" placeholder="抽奖概率"></el-input>
      </el-form-item>
      <el-form-item label="角度" prop="angle">
        <el-input v-model="dataForm.angle" :disabled="disabled" placeholder="抽奖转盘的角度"></el-input>
      </el-form-item>
      <el-form-item label="批次" prop="batch">
        <el-input v-model="dataForm.batch" :disabled="disabled" placeholder="批次"></el-input>
      </el-form-item>
      <el-form-item label="热销" prop="angle">
        <el-radio-group v-model="dataForm.isHot" :disabled="disabled">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动卡券" prop="batch">
        <el-radio-group v-model="dataForm.activityOnly" :disabled="disabled">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
        <span>（活动卡券不显示在正常购买卡券列表中）</span>
      </el-form-item>
      <el-form-item label="允许直接获取卡券" prop="angle">
        <el-radio-group v-model="dataForm.consumerGain" :disabled="disabled">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="卡券描述" prop="cardDesc">
        <ueditor v-model="dataForm.cardDesc" :disabled="disabled" placeholder="描述"></ueditor>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        cardCategoryList: [],
        dataForm: {
          id: 0,
          name: '',
          backColor: '',
          upImg: '',
          downImg: '',
          subsidy: '',
          categoryId: '',
          imgUrl: '',
          cardDesc: '',
          cardNumber: '',
          marketPrice: '',
          retailPrice: '',
          backImg: '',
          wechatPrice: '',
          isOnSale: '',
          probability: '',
          angle: '',
          expireDay: '',
          isShow: '',
          sort: '',
          tag: '',
          isHot: '',
          activityOnly: '',
          consumerGain: '',
          summary: ''
        },
        tags: [],
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          stint: [
            {required: true, message: '卡券购买限制未填', trigger: 'blur'}
          ],
          cardNumber: [
            {required: true, message: '卡券库存未填', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/card/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.card
                this.dataForm.tag = this.dataForm.tag.split(',')
              }
            })
          }
        })
        this.getCardCategoryList()
        this.getTagList()
      },
      getCardCategoryList () {
        this.$http({
          url: '/mall/cardcategory/queryAll',
          method: 'get',
          params: {
          }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.cardCategoryList = data.list
          } else {
            this.cardCategoryList = []
          }
        })
      },
      getTagList () {
        this.$http({
          url: '/mall/tag/queryAll',
          method: 'get',
          params: {
            type: 1
          }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.tags = data.list
          } else {
            this.tags = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              if(!(typeof this.dataForm.tag == 'string')){
                this.dataForm.tag = this.dataForm.tag.join(',')
              }
              this.$http({
                url: `/mall/card/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      },
      typeUpdata () {
        this.$forceUpdate()
      }
    }
  }
</script>
